package cn.darkhorse.mesadmin.controller.bigscreen;

import cn.darkhorse.device.dao.BigScreenDao;
import cn.darkhorse.device.vo.StatisticsPastSevenDaysVO;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电表控制器
 * @author yuanjun
 * @since 20231106
 */
@RestController()
@RequestMapping("/external/meter")
public class ExternalController {

    @Resource
    private BigScreenDao bigScreenDao;
    /**
     *    0重量
     */
    private static final String ZERO = "0";
    private static final String SEPARATOR = ".";
    private static final String DAYYIELD = "dayyield";
    private static final String MONTH_DAY = "month_day";
    private static final String DAYWEIGHT = "dayweight";
    
    /**
     * 近七日耗电量
     * @return
     */
    @RequestMapping("/weekly-consumption")
    public AjaxResult getWeeklyConsumption() {
        List<StatisticsPastSevenDaysVO> statisticsList = new ArrayList<>();
        List<StatisticsPastSevenDaysVO> statisticss = bigScreenDao.electricityPastSevenDays();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 7; i++) {
            LocalDateTime localDateTime = now.plusDays(-6+i);
            String key = localDateTime.getMonthValue()+SEPARATOR+localDateTime.getDayOfMonth();
            boolean isExit = false;
            if(!statisticss.isEmpty()){
                for(StatisticsPastSevenDaysVO ps:statisticss){
                    if(ps.getMonthDay().equals(key)){
                        statisticsList.add(ps);
                        isExit = true;
                        break;
                    }
                }
            }
            if(!isExit){
                StatisticsPastSevenDaysVO vo = new StatisticsPastSevenDaysVO();
                vo.setMonthDay(key);vo.setNumericalValue(ZERO);
                statisticsList.add(vo);
            }
        }
    
        return AjaxResult.success(statisticsList);
    }

    /**
     * 近7日产量
     * @return
     */
    @RequestMapping("/weekly-production-statistics")
    public AjaxResult getWeeklyProductionStatistics() {
        Map<String, List<StatisticsPastSevenDaysVO>> result = new HashMap<>();
        List<StatisticsPastSevenDaysVO> packagesList = new ArrayList<>();
        List<StatisticsPastSevenDaysVO> weightList = new ArrayList<>();
        List<Map<String, Object>> statisticss = bigScreenDao.productionPastSevenDays();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 7; i++) {
            LocalDateTime localDateTime = now.plusDays(-6+i);
            String key = localDateTime.getMonthValue()+SEPARATOR+localDateTime.getDayOfMonth();
            boolean isExit = false;
            if(!statisticss.isEmpty()){
                for(Map<String, Object> ps:statisticss){
                    if(ps.get(MONTH_DAY).equals(key)){
                        StatisticsPastSevenDaysVO vo = new StatisticsPastSevenDaysVO();
                        vo.setMonthDay(key);vo.setNumericalValue((ps.get(DAYYIELD) == null || StringUtils.isBlank(ps.get(DAYYIELD).toString())) ? ZERO:ps.get(DAYYIELD).toString());
                        packagesList.add(vo);
                        StatisticsPastSevenDaysVO vo2 = new StatisticsPastSevenDaysVO();
                        vo2.setMonthDay(key);vo2.setNumericalValue((ps.get(DAYWEIGHT) == null || StringUtils.isBlank(ps.get(DAYYIELD).toString())) ? ZERO:ps.get(DAYWEIGHT).toString());
                        weightList.add(vo2);
                        isExit = true;
                        break;
                    }
                }
            }
            if(!isExit){
                StatisticsPastSevenDaysVO vo = new StatisticsPastSevenDaysVO();
                vo.setMonthDay(key);vo.setNumericalValue(ZERO);
                packagesList.add(vo);
                StatisticsPastSevenDaysVO vo2 = new StatisticsPastSevenDaysVO();
                vo2.setMonthDay(key);vo2.setNumericalValue(ZERO);
                weightList.add(vo2);
            }
        }
        result.put("包数",packagesList);
        result.put("毛重",weightList);
        return AjaxResult.success(result);
    }

    /**
     * 获取设备mqtt数据
     * @return
     */
    @RequestMapping("/mqtt/view")
    public AjaxResult view(String deviceId) {
        if(StringUtils.isBlank(deviceId)){
            return AjaxResult.error("缺失参数");
        }
        String sql="select * from mqtt_live_data where device_id='"+deviceId+"' ";
        Map<String, Object> stringObjectMap = SqlRunner.db().selectOne(sql);
        Object data="";
        if(null!=stringObjectMap&&stringObjectMap.containsKey("data")){
            data=stringObjectMap.get("data");
        }
        return AjaxResult.success(data);
    }

    @NotNull
    private List<DayVO> getMockData() {
        List<DayVO> list = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 7; i++) {
            DayVO vo = new DayVO();
            String date = now.plusDays(1 * i).format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD));
            BigDecimal num = new BigDecimal(200 + Math.random() * 100);
            vo.setDate(date);
            vo.setNum(num.setScale(2, RoundingMode.HALF_UP));
            list.add(vo);
        }
        return list;
    }

    /**
     * 图表日数据
     */
    @Data
    private class DayVO {
        private String date;
        private BigDecimal num;
    }
    
    
    public static void main (String [] args){
    
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < 7; i++) {
            LocalDate localDateTime = now.plusDays(-1).toLocalDate();
            localDateTime.getMonth();
            System.out.println(localDateTime);
            System.out.println(localDateTime.getMonthValue());
            System.out.println(localDateTime.getDayOfMonth());
       
        }
        
    }
    
    
    
}
